package com.xiaoxi.iteratormode.core.iterator;

import com.xiaoxi.iteratormode.core.Iterator;
import com.xiaoxi.iteratormode.core.aggregate.BookSelf;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.iteratormode.core
 * @ClassName BookSelfIterator
 * @Description TODO
 * @Date 2023/3/7 10:53
 * @Version 1.0
 */
public class BookSelfIterator implements Iterator {

    private BookSelf bookSelf;
    private int index;
    private int size;

    public BookSelfIterator(BookSelf bookSelf) {
        this.bookSelf = bookSelf;
        this.index = 0;
        this.size = bookSelf.getLength();
    }

    @Override
    public boolean hasNext() {
        if(index < bookSelf.getLength()) {
            return true;
        }
        index = 0;
        return false;
    }

    @Override
    public Object next() {
        return bookSelf.getChatAt(index++).getName();
    }

    @Override
    public boolean hasPrevious() {
        if(size > 0) {
            return true;
        }
        size = bookSelf.getLength();
        return false;
    }

    @Override
    public Object previous() {
        return bookSelf.getChatAt(--size).getName();
    }
}
